App({
    onLaunch: function() {
        // 登录
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                wx.request({
                    url: this.globalData.ApiUrl + '/index.php?act=connect&op=wechat_jscode',
                    data: {
                        js_code: res.code
                    },
                    method: 'GET',
                    dataType: 'json',
                    success: (res) => {
                        // this.globalData.unionid = res.data.datas.unionid;
                        this.globalData.openid = res.data.datas.openid;
                        this.globalData.session_key = res.data.datas.session_key;
                    },
                });
            }
        });
        //检查是否存在新版本
        wx.getUpdateManager().onCheckForUpdate(function(res) {
            // 请求完新版本信息的回调
            if (res.hasUpdate) { //如果有新版本
                // 小程序有新版本，会主动触发下载操作（无需开发者触发）
                wx.getUpdateManager().onUpdateReady(function() { //当新版本下载完成，会进行回调
                        wx.showModal({
                            title: '更新提示',
                            content: '新版本已经准备好，单击确定重启应用',
                            showCancel: false,
                            success: function(res) {
                                if (res.confirm) {
                                    // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                                    wx.getUpdateManager().applyUpdate();
                                }
                            }
                        })

                    })
                    // 小程序有新版本，会主动触发下载操作（无需开发者触发）
                wx.getUpdateManager().onUpdateFailed(function() { //当新版本下载失败，会进行回调
                    wx.showModal({
                        title: '提示',
                        content: '检查到有新版本，但下载失败，请检查网络设置',
                        showCancel: false,
                    })
                })
            }
        });
        // 查看是否授权
        var self = this;
        wx.getSetting({
                success(res) {
                    if (res.authSetting['scope.userInfo']) {
                        // 已经授权
                        self.globalData.authorized = true
                    } else {
                        self.globalData.authorized = false
                    }
                }
            })
            //获取屏幕尺寸
        this.screenSize();
    },
    screenSize: function() {
        var self = this;
        wx.getSystemInfo({
            success: res => {
                self.globalData.sw = res.screenWidth;
                self.globalData.sh = res.screenHeight;
                self.globalData.ww = res.windowWidth;
                self.globalData.wh = res.windowHeight;
            }
        })
    },
    bezier: function(points, time) {
        var bezier_point = [];
        var points_D = [];
        var points_E = [];
        const dist_AB = Math.sqrt(Math.pow(points[1]['x'] - points[0]['x'], 2) + Math.pow(points[1]['y'] - points[0]['y'], 2));
        const dist_BC = Math.sqrt(Math.pow(points[2]['x'] - points[1]['x'], 2) + Math.pow(points[2]['y'] - points[1]['y'], 2));
        const move_AD = dist_AB / time;
        const move_BE = dist_BC / time;
        const tan_AB = (points[1]['y'] - points[0]['y']) / (points[1]['x'] - points[0]['x']);
        const tan_BC = (points[2]['y'] - points[1]['y']) / (points[2]['x'] - points[1]['x']);
        const radius_AB = Math.atan(tan_AB);
        const radius_BC = Math.atan(tan_BC);
        for (var i = 1; i <= time; i++) {
            var dist_AD = move_AD * i;
            var dist_BE = move_BE * i;
            var point_D = {};
            if (points[0]['x'] - points[2]['x'] > 0) {
                point_D['x'] = points[0]['x'] - dist_AD * Math.cos(radius_AB);
                point_D['y'] = points[0]['y'] - dist_AD * Math.sin(radius_AB);
            } else {
                point_D['x'] = dist_AD * Math.cos(radius_AB) + points[0]['x'];
                point_D['y'] = dist_AD * Math.sin(radius_AB) + points[0]['y'];
            }
            points_D.push(point_D);
            var point_E = {};
            if (points[0]['x'] - points[2]['x'] > 0) {
                point_E['x'] = points[1]['x'] - dist_BE * Math.cos(radius_BC);
                point_E['y'] = points[1]['y'] + Math.abs(dist_BE * Math.sin(radius_BC));
            } else {
                point_E['x'] = dist_BE * Math.cos(radius_BC) + points[1]['x'];
                point_E['y'] = dist_BE * Math.sin(radius_BC) + points[1]['y'];
            }
            points_E.push(point_E);
            var tan_DE = (point_E['y'] - point_D['y']) / (point_E['x'] - point_D['x']);
            var radius_DE = Math.atan(tan_DE);
            var dist_DE = Math.sqrt(Math.pow((point_E['x'] - point_D['x']), 2) + Math.pow((point_E['y'] - point_D['y']), 2));
            var dist_DF = dist_DE * (dist_AD / dist_AB);
            var point_F = {};
            if (points[0]['x'] - points[2]['x'] > 0) {
                point_F['x'] = point_D['x'] - dist_DF * Math.cos(radius_DE);
                point_F['y'] = point_D['y'] - dist_DF * Math.sin(radius_DE);
            } else {
                point_F['x'] = dist_DF * Math.cos(radius_DE) + point_D['x'];
                point_F['y'] = dist_DF * Math.sin(radius_DE) + point_D['y'];
            }
            bezier_point.push(point_F);
        }
        return {
            bezier_points: bezier_point
        }
    },
    globalData: {
        cachTempTime: 600000, //数据缓存时长10分钟单位毫秒
        // ApiUrl: "https://s.bdyoo.com/mobile",
        ApiUrl: "https://olcs1s.bdyoo.com/mobile",
        imageUrl: "https://i.bdyoo.com/data/resource/wechat/images/",
        session_key: '',
        version: '1.6.2'
    },
})